package com.glory.dgcpcpda.activity.cpc;

import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.LotStringListAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class WBPlasticpackActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    @BindView(R.id.etLotpp)
    FreeUI_ClearEditText etLot;

    @BindView(R.id.rvLotppList)
    RecyclerView rvLotList;

    private LotStringListAdapter mAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cpc_plasticpack;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvBaseSubmit.setVisibility(View.GONE);

        tvTitle.setText("键合塑封下料");

        initRecyclerView(rvLotList,this);

        mAdapter = new LotStringListAdapter(null);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mAdapter.isFirstOnly(false);
        rvLotList.setAdapter(mAdapter);

        initEdit(etLot, s -> {
            submit(s);
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked(View view) {
        switch (view.getId())   {
            case R.id.tvBaseSubmit:

                break;
        }
    }

    private void submit(String lotID){
        etLot.setText("");

        if(IsScan(lotID)){
            FreeApi_DialogUtils.getConfirmDialog(this, "确定","已经扫描过了", () -> {
                etLot.requestFocus();
            }).show(getSupportFragmentManager(),"提示");
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<JsonObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JsonObject>>
                (new TypeToken<BaseResponseBean<JsonObject>>() {
                }.getType(), getSupportFragmentManager(), WBPlasticpackActivity.this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JsonObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                    //showToast("上报成功",true);
                    mAdapter.addData(lotID + " - 提交成功！");
                    if(mAdapter.getItemCount() > 8){
                        mAdapter.remove(0);
                    }
                }
                etLot.setFocusable(true);
                etLot.setFocusableInTouchMode(true);
                etLot.requestFocus();
            }

            @Override
            public void onError(Response<BaseResponseBean<JsonObject>> response) {
                FreeApi_DialogUtils.getConfirmDialog(WBPlasticpackActivity.this, "确定",response.body().getResponse().getBody().get("RESULT").getAsString(), () -> {
                    etLot.requestFocus();
                }).show(getSupportFragmentManager(),"提示");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("LOTID",lotID);
        NetUtils.requestPostCPC("CPC.WBPlasticpack","WBPlasticpack",WBPlasticpackActivity.this,map,callBack);
    }

    private boolean IsScan(String lotid){
        if(mAdapter.getItemCount() == 0){
            return  false;
        }
        for (int i = 0;i < mAdapter.getItemCount();i ++){
            String item = mAdapter.getItem(i);
            if(item.startsWith(lotid)){
                return true;
            }
        }
        return  false;
    }

}